package com.cowmooed.controller;

import com.cowmooed.dto.GenerationRecordsDTO;
import com.cowmooed.dto.GenerationRecordsPageQueryDTO;
import com.cowmooed.entity.ModelEvaluationReport;
import com.cowmooed.entity.PageResult;
import com.cowmooed.entity.Result;
import com.cowmooed.service.GenerationRecordsService;
import com.cowmooed.service.ModelEvaluationReportService;
import com.cowmooed.vo.ModelEvaluationReportQueryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 模型控制器
 * 处理3D模型相关的请求
 */
@Api(tags = "模型管理")
@RestController
@RequestMapping("/model")
public class ModelController {

    @Autowired
    private GenerationRecordsService generationRecordsService;
    
    @Autowired
    private ModelEvaluationReportService modelEvaluationReportService;
    
    /**
     * 分页查询当前登录用户的模型记录
     * @param generationRecordsPageQueryDTO 分页查询参数
     * @return 分页查询结果
     */
    @ApiOperation(value = "分页查询用户模型记录", notes = "分页查询当前登录用户isDisplay为1的模型记录")
    @PostMapping("/pageQueryRecords")
    public Result pageQueryRecords(@RequestBody GenerationRecordsPageQueryDTO generationRecordsPageQueryDTO) {
        try {
            // 调用服务层方法进行分页查询
            PageResult pageResult = generationRecordsService.pageQueryUserRecords(generationRecordsPageQueryDTO);
            return Result.success(pageResult);
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }

    /**
     * 批量根据userId、taskId和requestId更新生成记录
     * @param generationRecordsDTOList 更新数据列表
     * @return 批量更新结果
     */
    @ApiOperation(value = "批量更新生成记录", notes = "批量根据userId和requestId更新数据库表中的生成记录")
    @PostMapping("/batchUpdateRecords")
    public Result batchUpdateRecords(@RequestBody List<GenerationRecordsDTO> generationRecordsDTOList) {
        try {
            // 调用服务层方法进行批量更新
            generationRecordsService.batchUpdateRecordsByUserIdAndTaskIdAndRequestId(generationRecordsDTOList);

            return Result.success();
        } catch (Exception e) {
            return Result.error("批量更新失败: " + e.getMessage());
        }
    }

    /**
     * 批量删除生成记录
     * @param ids 记录ID列表
     * @return 删除结果
     */
    @ApiOperation(value = "批量删除生成记录", notes = "批量根据记录ID删除数据库表中的生成记录")
    @DeleteMapping("/batchDeleteRecords/{ids}")
    public Result batchDeleteRecords(@PathVariable List<Long> ids) {
        try {
            // 调用服务层方法进行批量删除
            generationRecordsService.batchDeleteRecordsByIds(ids);

            return Result.success();
        } catch (Exception e) {
            return Result.error("批量删除失败: " + e.getMessage());
        }
    }
    
    /**
     * 查询当前登录用户的所有3D模型评估报告
     * @return 评估报告列表
     */
    @ApiOperation(value = "查询所有评估报告", notes = "查询当前登录用户的所有3D模型评估报告")
    @GetMapping("/queryAllEvaluationReports")
    public Result queryAllEvaluationReports() {
        try {
            // 调用服务层方法查询所有评估报告
            List<ModelEvaluationReportQueryVO> reports = modelEvaluationReportService.queryAllReportsByCurrentUser();
            return Result.success(reports);
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }
    
    /**
     * 将评估报告标记为已读
     * @param id 报告ID
     * @return 操作结果，包含标记后的完整报告信息
     */
    @ApiOperation(value = "标记评估报告为已读", notes = "根据报告ID将评估报告标记为已读，并返回完整报告信息")
    @PutMapping("/markReportAsRead/{id}")
    public Result markReportAsRead(@PathVariable Long id) {
        try {
            // 调用服务层方法标记报告为已读
            ModelEvaluationReport report = modelEvaluationReportService.markReportAsRead(id);
            if (report != null) {
                return Result.success(report);
            } else {
                return Result.error("标记失败，报告不存在或无权限");
            }
        } catch (IllegalArgumentException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("标记失败: " + e.getMessage());
        }
    }

}